﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
using System.IO;

public partial class user_user_pic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        else
        {
            TUser.CheckUser(this);
            //btnSave.Attributes.Add("OnClick", "javascript:return chk();");

            string sid = this.Session["ecrm_uid"].ToString();
            uid.Text = sid;
            TUser oUser = new TUser(sid);
            oUser.Select();
            string mypic = oUser.MyPic;

            if (mypic == "")
            {
                picfile.Text = "<img src=\"./pics/user.jpg\" border=0 width=48 height=48>";
            }
            else
            {
                picfile.Text = "<img src=\"./pics/" + mypic + "\" border=0 width=48 height=48>";
            }

        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string sLocalFile, sServerPath;

        TUser oUser = new TUser();

        sLocalFile = Path.GetFileName(headpic.PostedFile.FileName);
        sServerPath = "./pics/";
        if ((sLocalFile.Length > 0) && (headpic.PostedFile.ContentLength > 0))
        {
            //先删除
            DeleteFile("./pics/" + uid.Text+sLocalFile);
            //存入数据库
            //上传文件
            headpic.PostedFile.SaveAs(Server.MapPath(sServerPath + uid.Text+sLocalFile));
            mypicfile.Text = sLocalFile;
            //刷新
            picfile.Text = "<img src=\"./pics/" + uid.Text + sLocalFile + "\" border=0 width=48 height=48>";


        }
    }
    // 删除文件
    private void DeleteFile(string Filename)
    {
        try
        {
            System.IO.File.Delete(Server.MapPath(Filename));
        }
        catch
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("user_myself.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        TUser oUser = new TUser(uid.Text);
        oUser.MyPic = uid.Text + mypicfile.Text;
        oUser.Update_pic();
        this.Response.Redirect("user_myself.aspx");
    }
}
